package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game59;
import com.huike.game.listview.PullRefresh;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity59 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private PullRefresh  lv_game59;
    private ListViewAdapter adapter;
    private ProgressDialog show;
    private RequestQueue queue;
    private Game59 json;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game59);
        queue = Volley.newRequestQueue(this);
        initView();
        initDate();

        lv_game59.setOnRefreshLisener(new PullRefresh.onRefreshLisener() {
            @Override
            public void onRefresh() {
                getDate();
            }
        });


    }



    private void initDate() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "logQuery", new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();
                json = new Gson().fromJson(jsonObject.toString(), Game59.class);
                adapter = new ListViewAdapter();
                lv_game59.setAdapter(adapter);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Log.e("---",volleyError.getMessage() +"0");
                Log.e("---",volleyError.getLocalizedMessage() +"0");
                Log.e("---",volleyError.getNetworkTimeMs() +"0");
                Log.e("---",volleyError.getCause() +"0");
                Toast.makeText(GameActivity59.this, "数据加载失败！", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(request);

    }
    private void getDate() {
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "logQuery", new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject jsonObject) {
                json = new Gson().fromJson(jsonObject.toString(), Game59.class);
                lv_game59.setAdapter(adapter);
                lv_game59.setComplenerAnimation();//通知下拉刷新完成
                adapter.notifyDataSetChanged();



            }
        },null);
        queue.add(request);

    }

    private void show() {
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();

    }

    private void hide() {
        show.dismiss();
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        lv_game59 = findViewById(R.id.lv_game59);


        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
        tv_content_title.setText("日志查询");
        tv_left_text.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);

    }

    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return json.getData().size();
        }

        @Override
        public Object getItem(int i) {
            return json.getData().get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @SuppressLint("SimpleDateFormat")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity59.this, R.layout.lv_game59, null);
                holder = new ViewHolder(view);
                view.setTag(holder);

            } else {
                holder = (ViewHolder) view.getTag();
            }

            Game59.DataBean bean = json.getData().get(i);
            holder.tv_game59_ppm.setText(bean.getCo() +" ppm");
            holder.tv_game59_rh.setText(bean.getRain() + "%RH");
            holder.tv_game56_ug.setText(bean.getPm() +"ug/m3°C");
            holder.tv_game59_si.setText(bean.getSun() + "SI");
            holder.tv_game59_c.setText(bean.getTem() + "°C");

            SimpleDateFormat format = new SimpleDateFormat("yyy-mm-dd HH:mm:ss");
            long timeL = Long.parseLong(bean.getTiem());
            long dateL = timeL * 1000;
            String format1 = format.format(new Date(dateL));
            holder.tv_game59_time.setText(format1);

            return view;
        }


    }

    class ViewHolder {
        public View rootView;
        public TextView tv_game59_ppm;
        public TextView tv_game59_rh;
        public TextView tv_game56_ug;
        public TextView tv_game59_si;
        public TextView tv_game59_c;
        public TextView tv_game59_time;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_game59_ppm = (TextView) rootView.findViewById(R.id.tv_game59_ppm);
            this.tv_game59_rh = (TextView) rootView.findViewById(R.id.tv_game59_rh);
            this.tv_game56_ug = (TextView) rootView.findViewById(R.id.tv_game56_ug);
            this.tv_game59_si = (TextView) rootView.findViewById(R.id.tv_game59_si);
            this.tv_game59_c = (TextView) rootView.findViewById(R.id.tv_game59_c);
            this.tv_game59_time = (TextView) rootView.findViewById(R.id.tv_game59_time);
        }

    }

}
